package com.seng.logservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seng.logservice.entity.TbLoginLog;
import com.seng.logservice.service.TbLoginLogService;
import com.seng.utilscommon.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 登录日志表 前端控制器
 * </p>
 *
 * @author seng
 * @since 2020-07-31
 */
@Api(value = "登录日志controller类")
@RestController
@RequestMapping("/loginservice/tb-login-log")
@CrossOrigin
public class TbLoginLogController {

    @Autowired
    private TbLoginLogService tbLoginLogService;

    /**
     *
     * @param current 当前页数
     * @param size  当前条数
     * @param tbLoginLog 当前模糊查询对象
     * @return
     */
    @ApiOperation(value = "查询所有日志信息")
    @PostMapping("/findAll/{current}/{size}")
    public Result findAll(@PathVariable("current") Integer current,
                          @PathVariable("size") Integer size,
                          @RequestBody(required = false) TbLoginLog tbLoginLog) {

        System.out.println(tbLoginLog.toString());

        Page<TbLoginLog> page = new Page<>(current, size);
        QueryWrapper<TbLoginLog> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(tbLoginLog.getUsername())) {
            queryWrapper.like("username", tbLoginLog.getUsername());
        }
        if (!StringUtils.isEmpty(tbLoginLog.getIp())) {
            queryWrapper.like("ip", tbLoginLog.getIp());
        }
        if (!StringUtils.isEmpty(tbLoginLog.getLocation())) {
            queryWrapper.like("ip", tbLoginLog.getLocation());
        }
        IPage<TbLoginLog> loginlogPage = tbLoginLogService.page(page, queryWrapper);
        return Result.ok().data("loginlog", loginlogPage);
    }

    @ApiOperation(value = "逻辑删除用户")
    @DeleteMapping("deleteLoginlogById/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        System.out.println(id);
        boolean delete = tbLoginLogService.removeById(id);
        if (delete) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @ApiOperation(value = "批量删除用户")
    @PostMapping("deleteLoginlogByIdList")
    public Result deleteList(@RequestBody(required = false) List<Long> ids) {

        if (tbLoginLogService.removeByIds(ids)) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

}

